import os
import time

import requests
from requests.adapters import HTTPAdapter
import SiteData


class Req:
    # 发送HTTP请求时的HEAD信息，用于伪装为浏览器
    Head = {
        'Connection': 'Keep-Alive',
        'Accept': 'text/html, application/xhtml+xml, */*',
        'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
        'Accept-Encoding': 'gzip, deflate',
        'User-Agent': 'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
    }
    # 请求超时及重试设定
    connTimeout = 2  # connect timeout
    loadTimeout = 3  # load timeout
    reqSession = requests.Session()  # page request session
    reqSession.mount('http://', HTTPAdapter(max_retries=3))
    reqSession.mount('https://', HTTPAdapter(max_retries=3))
    picsTimeout = 5  # pics request timeout
    picSession = requests.Session()  # pics request session
    picSession.mount('http://', HTTPAdapter(max_retries=3))
    picSession.mount('https://', HTTPAdapter(max_retries=3))


def download_pic(path_name, pic_name, pic_url):
    # 图片分page存放
    pic_path = './pics/{}'.format(path_name)
    if not os.path.exists(pic_path):
        os.makedirs(pic_path)
    # print('Downloading pic: {}/{}  From: {}'.format(pic_path, pic_name, pic_url))
    try:
        pic_file = Req.picSession.get(pic_url, headers=Req.Head, verify=True,
                                      timeout=(Req.picsTimeout, Req.loadTimeout))
        pic_file.raise_for_status()
        fp = open("{}/{}".format(pic_path, pic_name), 'wb')
        fp.write(pic_file.content)
        fp.close()
    except requests.RequestException as e:
        print(e)
    pass


def phase_page(site: SiteData.Site, page_start, page_end):
    time_start = time.time()
    for page_num in range(page_start, page_end):
        page_url = site.page_format.format(page_num)
        print('Downloading: {}/{}, Time used:{}'
              .format(page_num, page_end, time.time()-time_start))
        try:
            response = Req.reqSession.get(page_url, headers=Req.Head, verify=True,
                                          timeout=(Req.connTimeout, Req.loadTimeout))
            print(site.phasePage(response))
        except requests.RequestException as e:
            print(e)
            continue
